package com.ezjie.ielts.core.spfs;

import java.util.Calendar;
import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;

import com.ezjie.ielts.application.YsApplication;
import com.ezjie.ielts.util.DateUtil;

/**
 * SharedPreferences的工具类
 * 
 * @author renyangyang
 * 
 */
public class SharedPrefHelper {
	/**
	 * SharedPreferences的名字
	 */
	private static final String SP_FILE_NAME = "project";
	private static SharedPrefHelper sharedPrefHelper = null;
	private static SharedPreferences sharedPreferences;

	private static final String ISFIRSTIN_TAG = "isFirstIn";
	private static final String IS_LOGIN = "isLogin";// 是否登录
	/** 每天练习目标数量 */
	private static final String DAILY_NUM = "daily_num";
	/** 随机出题 */
	private static final String IS_RAND = "is_rand";
	private static final String FINISH_NUM = "finish_num";

	public static synchronized SharedPrefHelper getInstance() {
		if (null == sharedPrefHelper) {
			sharedPrefHelper = new SharedPrefHelper();
		}
		return sharedPrefHelper;
	}

	private SharedPrefHelper() {
		sharedPreferences = YsApplication.myApplication.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
	}

	/**
	 * 清除缓存
	 * 
	 */
	public void clearCache() {
		sharedPreferences.edit().clear().commit();
	}

	/**
	 * 是否为第一次进入
	 * 
	 * @param flag
	 */
	public void setFirstIn(boolean flag) {
		sharedPreferences.edit().putBoolean(ISFIRSTIN_TAG, flag).commit();
	}

	public boolean isFirstIn() {
		return sharedPreferences.getBoolean(ISFIRSTIN_TAG, true);
	}

	/**
	 * 是否登录
	 * 
	 * @param flag
	 */
	public void setIsLogin(boolean flag) {
		sharedPreferences.edit().putBoolean(IS_LOGIN, flag).commit();
	}

	public boolean isLogin() {
		return sharedPreferences.getBoolean(IS_LOGIN, false);
	}

	/**
	 * 保存每个用户赞的时间
	 * 
	 * @param uid
	 */
	public void setZanTime(String uid) {
		sharedPreferences.edit().putString(uid + "zanTime", DateUtil.getCurrentTimeYYYYMMDD()).commit();
	}

	public String getZanTime(String uid) {
		return sharedPreferences.getString(uid + "zanTime", "2000-01-01");
	}

	/**
	 * 错题目标数量
	 * 
	 * @param daily_num
	 */
	public void setDailyNum(String uid, String daily_num) {
		sharedPreferences.edit().putString(uid + DAILY_NUM, daily_num).commit();
	}

	public String getDailyNum(String uid) {
		return sharedPreferences.getString(uid + DAILY_NUM, "10");
	}

	/**
	 * 是否随机出题 0是顺序,1是随机
	 * 
	 * @param is_rand
	 */
	public void setIsRand(String uid, String is_rand) {
		sharedPreferences.edit().putString(uid + IS_RAND, is_rand).commit();
	}

	public String isRand(String uid) {
		return sharedPreferences.getString(uid + IS_RAND, "1");
	}

	/**
	 * 今日已完成错题数
	 * 
	 * @param finish_num
	 */
	public void setFinishNum(String uid, String finish_num) {
		sharedPreferences.edit().putString(uid + FINISH_NUM, finish_num).commit();
	}

	public String getFinishNum(String uid) {
		return sharedPreferences.getString(uid + FINISH_NUM, "0");
	}

	/**
	 * 设置read中所有的更新时间
	 * 
	 * @param key为所有接口的唯一标识
	 */
	public void setUpdatetime(String key) {
		Calendar calendar = Calendar.getInstance(Locale.getDefault());
		sharedPreferences.edit().putLong(key, calendar.getTime().getTime()).commit();
	}

	/**
	 * read中所有的更新时间是否大于6个小时
	 * 
	 * @param key为所有接口的唯一标识
	 */
	public boolean getUpdatetime(String key) {
		Calendar calendar = Calendar.getInstance(Locale.getDefault());
		long i = calendar.getTime().getTime() - sharedPreferences.getLong(key, 1);
		return i / (1000 * 60 * 60) >= 6 ? true : false;
	}

	/**
	 * 保存更新接口信息
	 * 
	 * @param key为所有接口的唯一标识
	 * @param data为要保存的数据
	 */
	public String getUpdateData(String key) {
		return sharedPreferences.getString(key, "");
	}

	public void putUpdateData(String key, String data) {
		sharedPreferences.edit().putString(data, data).commit();
	}
}
